package nels4561;

import java.util.Map;

import spacewar2.actions.SpacewarPurchaseEnum;
import spacewar2.objects.Base;
import spacewar2.objects.Ship;
import spacewar2.simulator.Toroidal2DPhysics;

public class PowerupPlanning {
	static final int SHIP_THRESHOLD = 3; /*	Number of ships needed before purchasing powerups
	 										other than ships and bases*/
	static final int BASE_THRESHOLD = 3; /*	Number of bases needed before purchasing powerups
											other than ships and bases*/
	static final int BASE_ENERGY_THRESHOLD = 500;
	static final int CLOSING_DISTANCE = 25;
	static final int MAX_SHIPS = 5;
	
	int numShips = 0; //number of our ships
	int numBases = 0; //number of our bases
	int shipCost = 0; //current cost of a ship
	int baseCost = 0; //current cost of a base
	int shipShieldCost = 0; //current cost of a ship shield
	int baseShieldCost = 0; //current cost of a base shield
	int doubleBaseHealingRateCost = 0; //current cost to double the healing rate of a base
	int playerMoney = 0; //current available money for the player
	
	Ship currentShip; //current ship we're evaluating
	Base currentBase; //base tied to the ship we're evaluating
	
	boolean isBaseUpgraded = false; //is this base upgraded?
	boolean isShipShielded = false; //does the ship have a shield?
	boolean isBaseShielded = false; //does the base have a shield? 
	boolean isEnemyCloseToShip = false; //is there a hostile enemy close to our ship?
	boolean isEnemyCloseToBase = false; //is there a hostile enemy close to our base?
	boolean otherLadder = false; //false -> self; true ->other
	
	PowerupPlanning(Toroidal2DPhysics space, Ship myShip, Base myBase)
	{
		//initialize class variables
		currentShip = myShip;
		currentBase = myBase;
		playerMoney = myBase.getTeam().getAvailableMoney();
		
		for(Ship ship: space.getShips())
		{
			if(ship.getTeamName() == myBase.getTeamName())
				numShips++;
			else if(ship.getTeamName() != "BeaconCollectorTeam" || ship.getTeamName() != "RandomTeam")
			{
				if(ship.getTeamName() == "AggressiveHeuristicMinerTeam")
					otherLadder = true;
				
				if(space.findShortestDistance(currentShip.getPosition(), ship.getPosition()) < CLOSING_DISTANCE)
					isEnemyCloseToShip = true;
				
				if(space.findShortestDistance(currentBase.getPosition(), ship.getPosition()) < CLOSING_DISTANCE)
					isEnemyCloseToBase = true;
			}
		}
		
		for(Base base: space.getBases())
		{
			if(base.getTeamName() == myBase.getTeamName())
			{
				numBases++;
			}
		}
		
		if(currentBase.getHealingIncrement() >= 2*Base.INITIAL_ENERGY_HEALING_INCREMENT)
			isBaseUpgraded = true;
		
		if(currentShip.isShielded())
			isShipShielded = true;
		
		if(currentBase.isShielded())
			isBaseShielded = true;
		
		shipCost = myBase.getTeam().getCurrentCost(SpacewarPurchaseEnum.SHIP);
		baseCost = myBase.getTeam().getCurrentCost(SpacewarPurchaseEnum.BASE);
		shipShieldCost = myBase.getTeam().getCurrentCost(SpacewarPurchaseEnum.POWERUP_SHIELD);
		baseShieldCost = myBase.getTeam().getCurrentCost(SpacewarPurchaseEnum.POWERUP_SHIELD);
		doubleBaseHealingRateCost = myBase.getTeam().getCurrentCost(SpacewarPurchaseEnum.POWERUP_DOUBLE_BASE_HEALING_SPEED);
		
		
		
	}
	
	/**
	 * Action for buying a new SHIP
	 * 
	 * PRECOND:	numShips <= numBases &&
	 * 			numShips < MAX_SHIPS &&
	 * 			playerMoney >= shipCost;
	 * EFFECT:	numShips++ &&
	 * 			playerMoney -= shipCost;
	 * */
	boolean buyNewShip()
	{
		if((numShips <= numBases) && (playerMoney >= shipCost) && numShips < MAX_SHIPS)
			return true;
		return false;
	}
	
	/**
	 * Action for buying a new BASE
	 * 
	 * PRECOND: numShips > numBases &&
	 * 			playerMoney >= baseCost;
	 * EFFECT:	numBases++ &&
	 * 			playerMoney -= baseCost;
	 * */
	boolean buyNewBase()
	{
		if((numShips > numBases) && (playerMoney >= baseCost))
			return true;
		return false;
	}
	
	/**
	 * Action for DOUBLING THE BASE REGENERATION RATE
	 * 
	 * PRECOND: !isBaseUpgraded &&
	 * 			numShips > SHIP_THRESHOLD &&
	 * 			numBases > BASE_THRESHOLD &&
	 * 			currentBase.getEnergy() < BASE_ENERGY_THRESHOLD &&
	 * 			playerMoney >= doubleBaseHealingRateCost;
	 * EFFECT:	isBaseUpgraded = true &&
	 * 			playerMoney -= doubleBaseHealingRateCost;
	 * */
	boolean buyDoubleBaseHealingRate()
	{
		if(!isBaseUpgraded && 
				(numShips > SHIP_THRESHOLD) && 
				(numBases > BASE_THRESHOLD) && 
				(currentBase.getEnergy() < BASE_ENERGY_THRESHOLD) &&
				(playerMoney >= doubleBaseHealingRateCost))
			return true;
		return false;
	}
	
	/**
	 * Action for buying a new SHIP SHIELD
	 * 
	 * PRECOND: isEnemyCloseToShip == true &&
	 * 			otherLadder == true &&
	 * 			playerMoney >= shipShieldCost;
	 * EFFECT:	isShipShielded = true &&
	 * 			playerMoney -= shipShieldCost;
	 * */
	boolean buyShipShield()
	{
		if(isEnemyCloseToShip && otherLadder && (playerMoney >= shipShieldCost))
			return true;
		return false;
	}
	
	/**
	 * Action for buying a new BASE SHIELD
	 * 
	 * PRECOND: isEnemyCloseToBase == true &&
	 * 			otherLadder == true &&
	 * 			playerMoney >= baseShieldCost;
	 * EFFECT:	isShipShielded = true && 
	 * 			playerMoney -= baseShieldCost;
	 * */
	boolean buyBaseShield()
	{
		if(isEnemyCloseToBase && otherLadder && (playerMoney >= baseShieldCost))
			return true;
		return false;
	}
}
